package com.utils.tabbutton
{
	import flash.display.FrameLabel;
	import flash.display.Scene;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 * 选项卡按钮
	 * @author Kevin
	 */
	public class TabButton extends EventDispatcher
	{
		private const ROLL_OVER	:int = 0;
		private const ROLL_OUT	:int = 1;
		private const SELECTED	:int = 2;
		private const DOWN		:int = 3;
		private const DISABLED	:int = 4;
		
		private const FRAME_LABELS:Array = ["over", "out", "selected", "down", "disabled"];
		
		private var _buttonView:MovieClip = null;				// 按钮
		private var _tabList:ITabList = null;					// 聚合子列表的管理类		
		private var _label:TextField = null;					// 按钮标签
		
		private var _isSelect:Boolean = false;					// 是否被选中
		private var _type:int = 1;
		private var _index:int = 1;
		
		// 标签文字颜色
		private var _labelColors:Array = null;
		
		private var _boldText:Array = null;
		private var _format:TextFormat = null;
		private var _info:String = null;
		
		/**
		 * 选项卡列表
		 * @param	userList
		 * @param	type
		 */
		public function TabButton(tabList:ITabList,buttonView:MovieClip)
		{
			super();
			
			_tabList = tabList;		
			_buttonView = buttonView;
			_buttonView.mouseChildren = false;
			_buttonView.gotoAndStop(1);
			
			if (_buttonView.label)
			{
				_label = _buttonView.label;
				_label.mouseEnabled = false;
				_label.selectable = false;
			}
			
			_format = new TextFormat();
			_format.font = "Arial";
			
			_labelColors = [0, 0, 0, 0, 0, 0];
			_boldText = [null, null, null, null, null];
			
			setStatus(ROLL_OUT);			
			
		}
		
		/**
		 * 设置颜色
		 * @param	overColor		鼠标移上时的颜色
		 * @param	outColor		鼠标移开时的颜色
		 * @param	selectedColor	按钮被选中时的颜色
		 * @param	downColor		按钮按下时的颜色
		 * @param	diabledColor	禁用时的颜色
		 */
		public function setLabelColor(overColor:int = 0, outColor:int = 0, selectedColor:int = 0,
									  downColor:int = 0, disabledColor:int = 0):void																
		{	
			if (_label == null)
			{
				return;
			}
			
			var list:Array = [overColor, outColor, selectedColor, downColor, disabledColor];			
			for (var i:int = 0; i < _labelColors.length; i++)
			{
				_labelColors[i] = (list[i] == 0)? _labelColors[i] : list[i];				
			}
			
			_format.color = outColor;
			_label.setTextFormat(_format);
		}
		
		public function setTextBold(over:Boolean = false, out:Boolean = false, selected:Boolean = false,
									  down:Boolean = false, disabled:Boolean = false):void
		{
			if (_label == null)
			{
				return;
			}
			
			var list:Array = [over, out, selected, down, disabled];
			for (var i:int = 0; i < _boldText.length; i++)
			{
				_boldText[i] = (list[i]);
			}
			_format.bold = out;
			_label.setTextFormat(_format);
		}
		
		/**
		 * 是否选择当前组
		 * @param	isSelect
		 */
		public function select(isSelect:Boolean):void
		{
			_isSelect = isSelect;
			if(_isSelect)
			{	
				this.mouseEnabled = !_isSelect;				 
				setStatus(SELECTED);
				_buttonView.parent && _buttonView.parent.addChild(_buttonView);
				return;
			}
			
			setStatus(ROLL_OUT);
			this.mouseEnabled = !_isSelect;
		}
		
		/**
		 * 是否激活按钮
		 */
		public function set mouseEnabled(value:Boolean):void
		{
			value ? addEvent() : removeEvent();
		}
		
		/**
		 * 添加事件侦听
		 */		
		private function addEvent():void 
		{
			if (_isSelect == false) setStatus(ROLL_OUT);			
			
			_buttonView.buttonMode = true;
			//
			_buttonView.addEventListener(MouseEvent.ROLL_OVER, overHandler);
			_buttonView.addEventListener(MouseEvent.ROLL_OUT, outHandler);
			_buttonView.addEventListener(MouseEvent.CLICK, clickHandler);
			
			//
			_buttonView.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
		}
		
		/**
		 * 移除事件侦听
		 */	
		private function removeEvent():void 
		{		
			if (_isSelect == false)	setStatus(DISABLED);			
			_buttonView.buttonMode = false;
			//
			_buttonView.removeEventListener(MouseEvent.ROLL_OVER, overHandler);
			_buttonView.removeEventListener(MouseEvent.ROLL_OUT, outHandler);
			_buttonView.removeEventListener(MouseEvent.CLICK, clickHandler);
			
			//
			_buttonView.removeEventListener(MouseEvent.MOUSE_DOWN, downHandler);
		}		
		
		/**
		 * 鼠标按下
		 * @param	e
		 */
		private function downHandler(e:MouseEvent):void 
		{
			setStatus(DOWN);
			
			//
			_buttonView.stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_DOWN));
		}
		
		/**
		 * 鼠标弹起
		 * @param	e
		 */
		private function upHandler(e:MouseEvent):void 
		{
			e.currentTarget.removeEventListener(e.type, arguments.callee);
			
			//
			setStatus(ROLL_OUT);
		}
		
		
		/**
		 * 鼠标点击
		 * @param	e
		 */
		private function clickHandler(e:MouseEvent):void 
		{
			if (_tabList)_tabList.currentItem = this;
			setStatus(SELECTED);	
			//_buttonView.parent && _buttonView.parent.addChild(_buttonView);
			//
			dispatchEvent(new MouseEvent(MouseEvent.CLICK));
		}
		
		/**
		 * 鼠标移开事件
		 * @param	e
		 */
		private function outHandler(e:MouseEvent):void 
		{
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OUT));
			if (_isSelect) return;
			setStatus(ROLL_OUT);
		}
		
		/**
		 * 鼠标移上事件
		 * @param	e
		 */
		private function overHandler(e:MouseEvent):void 
		{
			dispatchEvent(new MouseEvent(MouseEvent.MOUSE_OVER));
			if (_isSelect) return;
			setStatus(ROLL_OVER);
		}	
		
		/**
		 * 设置按钮效果
		 * @param	type
		 */
		private function setStatus(type:int):void
		{				
			if (checkFrame(FRAME_LABELS[type]) == false) return;			
			
			//
			if (_label != null)
			{
				_format.color = _labelColors[type];	
				_format.bold = _boldText[type];
				_label.setTextFormat(_format);
			}
			
			//			
			_buttonView.gotoAndStop(FRAME_LABELS[type]);
		}
		
		/**
		 * 检测标签的有效性
		 * @param	frameLabel
		 * @return
		 */
		private function checkFrame(frameLabel:String):Boolean		
		{
			var scene:Scene = _buttonView.scenes[0];
			var labelList:Array = scene.labels;
			var label:FrameLabel = null;			
			for (var i:int = 0; i < labelList.length; i++)
			{
				label = labelList[i];
				if (label.name != frameLabel) continue;
				return true;
			}
			
			//
			return false;
		}
		
		//------------------------------------------------------------------------------------------------------//
		//											Getters & Setters											//
		//------------------------------------------------------------------------------------------------------//
		/**
		 * 选项卡按钮的横坐标
		 */
		public function get x():Number { return _buttonView.x; }		
		public function set x(value:Number):void 
		{			
			_buttonView.x = value;
		}
		
		/**
		 * 选项卡按钮的竖坐标
		 */
		public function get y():Number { return _buttonView.y; }		
		public function set y(value:Number):void 
		{
			_buttonView.y = value;
		}
		
		/**
		 * 设置透明度属性
		 */
		public function get alpha():Number { return _buttonView.alpha; }
		public function set alpha(value:Number):void
		{
			_buttonView.alpha = value;
		}
		
		/**
		 * 选项卡按钮的宽度
		 */
		public function get width():Number { return _buttonView.width; }			
		
		/**
		 * 选项卡按钮的高度
		 */
		public function get height():Number { return _buttonView.height; }		
		
		/**
		 * 选项卡按钮标签文字
		 */
		public function get label():String { return _label != null ? _label.text : ""; }		
		public function set label(value:String):void 
		{			
			if (_label == null)
				return
			_label.text = value;
			_label.setTextFormat(_format);
		}
		
		/**
		 * 类型
		 */
		public function get type():int { return _type; }	
		public function set type(value:int):void
		{
			_type = value;
		}
		
		public function get visible():Boolean { return _buttonView.visible; }		
		public function set visible(value:Boolean):void 
		{
			_buttonView.visible = value;			
			_buttonView.stop();
		}
		
		/**
		 * 是否粗体显示
		 */			
		public function set isBold(value:Boolean):void 
		{
			_format.bold = value;
		}
		
		/**
		 * 字体大小
		 */
		public function set fontSize(value:Number):void
		{
			_format.size = value;
		}
		
		/**
		 * 扩展索引号
		 */
		public function get index():int { return _index; }		
		public function set index(value:int):void 
		{
			_index = value;
		}
		/**
		 * 扩展信息
		 */
		public function get info():String { return _info; }
		
		public function set info(value:String):void 
		{
			_info = value;
		}
		
	}

}
